<?php
	class Admin_NewsController extends Admin_BaseController {

		public function getNews() {
			return View::make("admin.news.index")->with(array(
					'news' => News::orderBy('date', 'DESC')->paginate(10)
				));
		}

		public function getAddNews() {
			return View::make("admin.news.add");
		}

		public function postAddNews() {
			$rules = array(
				'titre' => 'required|between:1,300',
				'permalien' => 'required|between:1,300',
				'description' => 'required|between:1,500',
				'contenu' => 'required'
			);

			$validator = Validator::make(Input::all(), $rules);

			if ($validator->fails())
            {
				return Redirect::back()->withInput()->withErrors($validator);	
            }
            else {
            	$news = new News;
				$news->user_id = Auth::user()->id;
				$news->title = Input::get('titre');
				$news->permalien = Input::get('permalien');
				$news->description = Input::get('description');
				$news->contenu = Input::get('contenu');
				$news->save();

				return Redirect::route('admin-news');
            }
		}

		public function getEditNews($id = null) {
			return View::make("admin.news.edit")->with(array(
					'news' => News::find($id)
				));
		}

		public function postEditNews($id = null) {
			$rules = array(
				'titre' => 'required|between:1,300',
				'description' => 'required|between:1,500',
				'contenu' => 'required'
			);

			$validator = Validator::make(Input::all(), $rules);

			if ($validator->fails())
            {
            	//dd($validator);
				return Redirect::back()->withInput()->withErrors($validator);	
            }
            else {
            	$news = News::find($id);
				$news->title = Input::get('titre');
				$news->description = Input::get('description');
				$news->contenu = Input::get('contenu');
				$news->save();

				return Redirect::route('admin-news');
            }
		}

		public function getDeleteNews($id = null) {
			return View::make("admin.news.delete")->with(array('id' => $id));
		}

		public function postDeleteNews($id = null) {
			News::destroy($id);
			return Redirect::route('admin-news');
		}
	}